from django.db import models
from django.contrib.auth.models import User


class TherapyPlan(models.Model):
    """疗愈方案表"""
    PLAN_TYPE_CHOICES = [
        ('cbt', '认知行为疗法'),
        ('mindfulness', '正念疗法'),
        ('art', '艺术疗愈'),
        ('exercise', '运动疗愈'),
        ('comprehensive', '综合方案'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    plan_name = models.CharField(
        max_length=200, 
        verbose_name='方案名称',
        db_comment='疗愈方案名称'
    )
    plan_type = models.CharField(
        max_length=50, 
        choices=PLAN_TYPE_CHOICES, 
        verbose_name='方案类型',
        db_comment='疗愈方案类型'
    )
    target_emotion = models.CharField(
        max_length=50, 
        verbose_name='目标情绪',
        db_comment='针对的情绪问题'
    )
    plan_content = models.JSONField(
        verbose_name='方案内容',
        db_comment='疗愈方案详细内容JSON'
    )
    duration_days = models.IntegerField(
        verbose_name='持续天数',
        db_comment='方案计划持续天数'
    )
    start_date = models.DateField(
        verbose_name='开始日期',
        db_comment='方案开始日期'
    )
    end_date = models.DateField(
        null=True, 
        blank=True, 
        verbose_name='结束日期',
        db_comment='方案结束日期'
    )
    progress = models.IntegerField(
        default=0, 
        verbose_name='完成进度',
        db_comment='方案完成进度百分比0-100'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否活跃',
        db_comment='方案是否活跃'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_therapy_plan'
        db_table_comment = '疗愈方案表'
        verbose_name = '疗愈方案'
        verbose_name_plural = '疗愈方案'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.plan_name}'


class TherapyActivity(models.Model):
    """疗愈活动记录表"""
    plan = models.ForeignKey(
        TherapyPlan, 
        on_delete=models.CASCADE, 
        related_name='activities',
        verbose_name='疗愈方案',
        db_comment='关联疗愈方案ID'
    )
    activity_name = models.CharField(
        max_length=200, 
        verbose_name='活动名称',
        db_comment='疗愈活动名称'
    )
    activity_type = models.CharField(
        max_length=50, 
        verbose_name='活动类型',
        db_comment='活动类型(呼吸练习/冥想/绘画/写作等)'
    )
    description = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='活动描述',
        db_comment='活动详细描述'
    )
    scheduled_time = models.DateTimeField(
        verbose_name='计划时间',
        db_comment='计划执行时间'
    )
    completed = models.BooleanField(
        default=False, 
        verbose_name='是否完成',
        db_comment='活动是否完成'
    )
    completed_at = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='完成时间',
        db_comment='实际完成时间'
    )
    duration_minutes = models.IntegerField(
        null=True, 
        blank=True, 
        verbose_name='持续时长',
        db_comment='活动持续时长(分钟)'
    )
    feedback = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='反馈',
        db_comment='用户对活动的反馈'
    )
    effectiveness_rating = models.IntegerField(
        null=True, 
        blank=True, 
        verbose_name='有效性评分',
        db_comment='活动有效性评分1-5分'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_therapy_activity'
        db_table_comment = '疗愈活动记录表'
        verbose_name = '疗愈活动'
        verbose_name_plural = '疗愈活动'
        ordering = ['scheduled_time']

    def __str__(self):
        return f'{self.plan.plan_name} - {self.activity_name}'


class CBTExercise(models.Model):
    """CBT认知重构练习表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    situation = models.TextField(
        verbose_name='情境',
        db_comment='引发情绪的情境描述'
    )
    automatic_thought = models.TextField(
        verbose_name='自动思维',
        db_comment='当时出现的自动化思维'
    )
    emotion = models.CharField(
        max_length=100, 
        verbose_name='情绪',
        db_comment='产生的情绪及强度'
    )
    evidence_for = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='支持证据',
        db_comment='支持该思维的证据'
    )
    evidence_against = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='反对证据',
        db_comment='反对该思维的证据'
    )
    alternative_thought = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='替代思维',
        db_comment='更合理的替代思维'
    )
    new_emotion = models.CharField(
        max_length=100, 
        null=True, 
        blank=True, 
        verbose_name='新情绪',
        db_comment='重构后的情绪及强度'
    )
    exercise_date = models.DateField(
        verbose_name='练习日期',
        db_comment='练习记录日期'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_cbt_exercise'
        db_table_comment = 'CBT认知重构练习表'
        verbose_name = 'CBT练习'
        verbose_name_plural = 'CBT练习'
        ordering = ['-exercise_date', '-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.exercise_date}'
